﻿using System;
using Saturn.AppSQLite;
using System.Data.SQLite;
using System.Data;

namespace Saturn.App.FormOper
{
    class FrmRelationPileOper
    {
        /// <summary>
        /// 扫描后添加数据库
        /// </summary>
        /// <param name="PileCode"></param>
        /// <param name="BoxCode"></param>
        /// <returns></returns>
        public int InsertPileRelation(string PileCode, string BoxCode)
        {
            try
            {
                // 查重
                string strQuery = string.Format(" select count(*) from PileRelation where PileCode='{0}' or BoxCode='{1}'", PileCode, BoxCode);
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.CommandText = strQuery;
                cmd.CommandType = CommandType.Text;
                if (int.Parse(SqliteHelper.ExecuteScalar(SqliteHelper.SqlConnection, cmd).ToString()) != 0)
                {
                    return -1;// 存在重复数据。
                }
                // 添加
                string strInsert = string.Format(" insert into PileRelation(PileCode,BoxCode) values('{0}','{1}')", PileCode, BoxCode);
                cmd = new SQLiteCommand();
                cmd.CommandText = strInsert;
                cmd.CommandType = CommandType.Text;
                return SqliteHelper.ExecuteNonQuery(SqliteHelper.SqlConnection, cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取需要上传手动关联的数据
        /// </summary>
        public DataTable GetPileRelation()
        {
            string strSql = " select * from PileRelation ";
            DataTable dt = new DataTable();
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.CommandText = strSql;
            cmd.CommandType = CommandType.Text;
            dt = SqliteHelper.ExecuteDataSet(SqliteHelper.SqlConnection, cmd).Tables[0];
            return dt;
        }

        /// <summary>
        /// 上传后删除本地关联数据
        /// </summary>
        /// <param name="PileCode"></param>
        /// <param name="BoxCode"></param>
        /// <returns></returns>
        public int DeletePileRelation(string PileCode, string BoxCode)
        {
            try
            {
                // 添加
                string strDel = string.Format(" Delete from PileRelation where PileCode='{0}' and BoxCode='{1}'", PileCode, BoxCode);
                SQLiteCommand cmd = new SQLiteCommand();
                cmd = new SQLiteCommand();
                cmd.CommandText = strDel;
                cmd.CommandType = CommandType.Text;
                return SqliteHelper.ExecuteNonQuery(SqliteHelper.SqlConnection, cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
